// i.r(t), i.d(t, { default: () => CurrentPanoMarker })
import Module from "./Module"
import SymbolList from "../SymbolList"
import * as THREE from "three"
class MarkerRenderer {
  scene: WebGLScene
  sweepData: SweepData
  material: THREE.MeshBasicMaterial
  mesh: THREE.Mesh<THREE.PlaneGeometry, THREE.MeshBasicMaterial>
  static planeGeometry: THREE.PlaneGeometry
  static floorOffset: number
  constructor(e: WebGLScene, t: THREE.Texture, i: SweepData, n: RenderLayer) {
    this.scene = e
    this.sweepData = i
    this.material = new THREE.MeshBasicMaterial({
      transparent: !0,
      map: t,
      depthWrite: !1
    })
    this.mesh = new THREE.Mesh(MarkerRenderer.planeGeometry, this.material)
    this.mesh.name = "CurrentPanoMarker"
    this.mesh.renderOrder = renderOrderEnum.panoMarker
    this.mesh.layers.mask = n.mask
    this.mesh.lookAt(this.mesh.position.clone().setY(1))
  }
  init() {}
  dispose() {
    this.material.map && this.material.map.dispose()
    this.mesh.geometry.dispose()
  }
  render() {
    const e = this.sweepData.currentAlignedSweepObject
    e && this.move(e.floorPosition)
  }
  activate() {
    this.scene.add(this.mesh)
  }
  deactivate() {
    this.scene.remove(this.mesh)
  }
  move(e) {
    this.mesh.position.copy(e).setY(e.y + MarkerRenderer.floorOffset)
  }
}
MarkerRenderer.floorOffset = 0.01
MarkerRenderer.planeGeometry = new THREE.PlaneGeometry(0.5, 0.5)
import TextureLoader from "../loader/TextureLoader"
import ViewmodeData from "../data/ViewmodeData"
import SweepData from "../data/SweepData"

import { TogglePanoMarkerCommand } from "../command/pano.command"

import SettingsData from "../data/SettingsData"
import renderOrderEnum from "../enum/renderOrder.enum"
import keyConst from "../constant/key.const"
import { isPanOrMesh } from "../utils/viewmode.util"
import viewmodeEnum from "../enum/viewmode.enum"
import EngineContext from "../EngineContext"
import WebGLRendererModule from "./WebGLRendererModule"
import WebGLScene from "../three/WebGLScene"
import { RenderLayer } from "../utils/RenderLayers"
export default class CurrentPanoMarker extends Module {
  state: { markerEnabled: boolean; transitionPromise: Promise<any>; commandOverride?: any }
  engine: EngineContext
  settingsData: SettingsData
  sweepData: SweepData
  viewmodeData: ViewmodeData
  markerRenderer: MarkerRenderer
  constructor() {
    super(...arguments)
    this.name = "current-pano-marker"
    this.state = { markerEnabled: !1, transitionPromise: null }
  }
  async init(e, t: EngineContext) {
    const i = TextureLoader("./images/puck_256_red.png")
    const n = (await t.getModuleBySymbol<WebGLRendererModule>(SymbolList.WEBGL_RENDERER)).getScene()
    const r = t.claimRenderLayer(this.name)
    const [o, d, u] = await Promise.all([t.market.waitForData(SettingsData), t.market.waitForData(SweepData), t.market.waitForData(ViewmodeData)])
    this.engine = t
    this.settingsData = o
    this.sweepData = d
    this.viewmodeData = u
    this.markerRenderer = new MarkerRenderer(n, i, d, r)
    this.bindings.push(
      t.commandBinder.addBinding(TogglePanoMarkerCommand, async ({ enabled: e }) => this.setCommandOverride(e)),
      this.viewmodeData.makeModeChangeSubscription(() => this.updateMarker()),
      this.sweepData.makeSweepChangeSubscription(() => this.updateMarker()),
      this.settingsData.onPropertyChanged(keyConst.rule.features.sweep_indicator, () => this.updateMarker())
    )
    this.updateMarker()
  }
  setCommandOverride(e) {
    this.state.commandOverride = e
    this.updateMarker()
  }
  async updateMarker() {
    const { commandOverride: e } = this.state,
      t = this.viewmodeData.currentMode,
      i = this.sweepData.currentAlignedSweepObject,
      n = !1 === this.settingsData.tryGetProperty(keyConst.rule.features.sweep_indicator, !0) || !1 === e || isPanOrMesh(t) || t === viewmodeEnum.Transition
    return this.toggleMarker(!!i && !n)
  }
  async toggleMarker(e: boolean) {
    const { markerEnabled: t, transitionPromise: i } = this.state
    if ((i && (await i), t === e)) return
    const n = e ? this.enableMarker() : this.disableMarker()
    this.state.transitionPromise = n.finally(() => {
      this.state.markerEnabled = e
      this.state.transitionPromise = null
    })
    return this.state.transitionPromise
  }
  async enableMarker() {
    return this.engine.addComponent(this, this.markerRenderer)
  }
  async disableMarker() {
    return this.engine.removeComponent(this, this.markerRenderer)
  }
}
